Socket
Socket
Sign inDemoInstall

human-format

Package Overview
Dependencies
0
Maintainers
3
Versions
21
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    human-format

Converts a number to/from a human readable string: `1337` ↔ `1.34kB`


Version published
Maintainers
3
Install size
13.2 kB
Created

Readme

Source

human-format

Build Status Dependency Status devDependency Status

Converts a number to/from a human readable string: 13371.34kB

Installation

Node & Browserify/Webpack

Installation of the npm package:

> npm install --save human-format

Then require the package:

var humanFormat = require('human-format');

Browser

You can directly use the build provided at unpkg.com:

<script src="https://unpkg.com/human-format@0.10/index.js"></script>

Usage

Formatting

humanFormat(1337)
//=> '1.34 k'

// The number of decimals can be changed.
humanFormat(1337, {
  decimals: 1
})
//=> '1.3 k'

// Units and scales can be specified.
humanFormat(65536, {
  scale: 'binary',
  unit: 'B'
})
//=> 64 kiB

// A custom separator can be specified.
humanFormat(1337, {
  separator: ' - '
})
//=> 1.34 - k

// Custom scales can be created!
var timeScale = new humanFormat.Scale({
  seconds: 1,
  minutes: 60,
  hours: 3600,
  days: 86400,
  months: 2592000,
})
humanFormat(26729235, { scale: timeScale })
//=> 10.31 months

// You can force a prefix to be used.
humanFormat(100, { unit: 'm', prefix: 'k' })
//=> 0.1 km

// You can access the raw result.
humanFormat.raw(100, { prefix: 'k' })
//=> {
//   prefix: 'k',
//   value: 0.09999999999999999 // Close value, not rounded.
// }

Parsing

humanFormat.parse('1.34 kiB', { scale: 'binary' })
//=> 1372.16

// Fallbacks when possible if the prefix is incorrectly cased.
humanFormat.parse('1 g')
// => 1000000000

// You can access the raw result.
humanFormat.parse.raw('1.34 kB')
//=> {
//  factor: 1000,
//  prefix: 'k',
//  unit: 'B',
//  value: 1.34
//}

Contributions

Contributions are very welcomed, either on the documentation or on the code.

You may:

  • report any issue you've encountered;
  • fork and create a pull request.

Contributors:

  • @djulien
  • @qrohlf
  • @Itay289
  • @sweetpi

License

ISC © Julien Fontanet

Keywords

FAQs

Last updated on 04 Oct 2018

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc